#!/bin/bash

get_gpio()
{
    local NET_NAME=$1
    local RET_VAL

    mapfile -t -d " " GPIO_INFO < <(gpiofind "$NET_NAME")
    if [ "${#GPIO_INFO[@]}" -ne 2 ]; then
        echo "get_gpio: can not find gpio, $NET_NAME" >&2
        return 1
    fi
    if ! RET_VAL=$(gpioget "${GPIO_INFO[0]}" "${GPIO_INFO[1]%$'\n'}") ; then
        echo "get_gpio: get ${NET_NAME} failed" >&2
        return 1
    fi
    echo "${RET_VAL}"
    return 0
}

set_gpio()
{
    local NET_NAME=$1
    local OUT_VAL=$2
    mapfile -t -d " " GPIO_INFO < <(gpiofind "$NET_NAME")
    if [ "${#GPIO_INFO[@]}" -ne 2 ]; then
        echo "set_gpio: can not find gpio, $NET_NAME"
        return 1
    fi

    echo -n "set_gpio: set $NET_NAME = $OUT_VAL"
    if ! gpioset "${GPIO_INFO[0]}" "${GPIO_INFO[1]%$'\n'}"="$OUT_VAL"; then
        echo " failed"
        return 1
    fi

    echo " success"
    return 0
}

wait_gpio_falling()
{
    local NET_NAME=$1
    local TIMEOUT_SEC=$2

    mapfile -t -d " " GPIO_INFO < <(gpiofind "$NET_NAME")
    if [ "${#GPIO_INFO[@]}" -ne 2 ]; then
        echo "wait_gpio_falling: can not find gpio, $NET_NAME"
        return 1
    fi

    timeout "$TIMEOUT_SEC" gpiomon --silent --falling-edge --num-events=1 "${GPIO_INFO[0]}" "${GPIO_INFO[1]%$'\n'}"
}

set_fan()
{
    FAN_ID=$1
    FAN_DUTY=$2
    SYSFA_PWM_PATH=""

    for file in /sys/devices/platform/pwm-fan"$FAN_ID"/hwmon/hwmon*/pwm1
    do
        if [ -e "$file" ]; then
            SYSFA_PWM_PATH="$file"
            break
        fi
    done

    if [ -z "$SYSFA_PWM_PATH" ]; then
        echo "set_fan: pwm file not found, chekc fan id ($FAN_ID)"
        return 1
    fi

    if [ "$FAN_DUTY" -lt 0 ] || [ "$FAN_DUTY" -gt 100 ]; then
        echo "set_fan: incorrect fan duty, $FAN_DUTY"
        return 1
    fi

    # convert duty (0-100) to pwm value (0-255)
    PWM_VAL=$(printf "%.0f" $((FAN_DUTY*255/100)))

    echo -n "set_fan: set fan$FAN_ID = $FAN_DUTY"
    if ! echo "$PWM_VAL" > "$SYSFA_PWM_PATH"; then
        echo " failed"
        return 1
    fi

    echo " success"
    return 0
}


is_sled_present()
{
    local SLED_ID=$1
    local SRV_NAME="xyz.openbmc_project.Inventory.Manager"
    local OBJ_PATH="/xyz/openbmc_project/inventory/system/chassis/presence/presence_sled${SLED_ID}"
    local INTF_NAME="xyz.openbmc_project.Inventory.Item"
    local PRST_VAL

    PRST_VAL=$(busctl get-property "${SRV_NAME}" "${OBJ_PATH}" "${INTF_NAME}" Present | awk '{print $2}')
    if [ "$PRST_VAL" != "true" ]; then
        # not present
        return 1
    fi

    # present
    return 0
}

spi2_mux_select()
{
    local SLED_INDEX="$1"
    if [ "$SLED_INDEX" = "0" ]; then
        set_gpio SEL_SPI2_MUX 1
        set_gpio SPI2_MUX1 1
        set_gpio SPI2_MUX2 1
        set_gpio SPI2_MUX3 1
    elif [ "$SLED_INDEX" = "1" ]; then
        set_gpio SEL_SPI2_MUX 0
        set_gpio SPI2_MUX1 0
        set_gpio SPI2_MUX2 1
        set_gpio SPI2_MUX3 1
    elif [ "$SLED_INDEX" = "2" ]; then
        set_gpio SEL_SPI2_MUX 1
        set_gpio SPI2_MUX1 0
        set_gpio SPI2_MUX2 1
        set_gpio SPI2_MUX3 1
    elif [ "$SLED_INDEX" = "3" ]; then
        set_gpio SEL_SPI2_MUX 0
        set_gpio SPI2_MUX1 1
        set_gpio SPI2_MUX2 0
        set_gpio SPI2_MUX3 1
    elif [ "$SLED_INDEX" = "4" ]; then
        set_gpio SEL_SPI2_MUX 1
        set_gpio SPI2_MUX1 1
        set_gpio SPI2_MUX2 0
        set_gpio SPI2_MUX3 1
    elif [ "$SLED_INDEX" = "5" ]; then
        set_gpio SEL_SPI2_MUX 0
        set_gpio SPI2_MUX1 1
        set_gpio SPI2_MUX2 1
        set_gpio SPI2_MUX3 0
    elif [ "$SLED_INDEX" = "6" ]; then
        set_gpio SEL_SPI2_MUX 1
        set_gpio SPI2_MUX1 1
        set_gpio SPI2_MUX2 1
        set_gpio SPI2_MUX3 0
    else
        echo "set_spi2_mux: unknow sled index ($SLED_INDEX)"
        return 1
    fi

    return 0
}

bind_spi2_pnor()
{
    local SLED_INDEX="$1"

    if ! spi2_mux_select "$SLED_INDEX"; then
        return 1
    fi

    if ! echo -n spi1.0 > /sys/bus/spi/drivers/spi-nor/bind; then
        echo "Error: flash bind failed"
        return 1
    fi

    return 0
}

unbind_spi2_pnor()
{
    echo -n spi1.0 > /sys/bus/spi/drivers/spi-nor/unbind
    spi2_mux_select 0
    return 0
}

find_mtd()
{
    m=$(grep -xl "$1" /sys/class/mtd/*/name)
    m=${m%/name}
    m=${m##*/}
    echo "$m"
}
